package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Library {

    private ArrayList<LibraryBook> bookList;
    private ArrayList<Movie> movieList;
    private InputStreamReader userInput;
    private User currentUser ;
    private boolean userLoggedIn = false;


    public void Start()
    {
        LoadBooks();
        LoadMovies();
        LoadUsers();
        ShowScreen();
        ShowMenu();
    }


    private void LoadBooks()
    {
        Book book1 = new Book("Food Guide Melbourne 2013", "John Doe", "12345zxcvb");
        Book book2 = new Book("Europe on a shoe string", "Anna Smith", "67890asdfg");
        Book book3 = new Book("World Airport Guide", "Mark William", "11111qwert");

        addBookToList(book1);
        addBookToList(book2);
        addBookToList(book3);
    }

    private void LoadMovies()
    {
        Movie movie = new Movie("Name", "Year","Director","Rating");
        Movie movie1 = new Movie("Fast 5","2011","someone","8");
        Movie movie2 = new Movie("Love Actually", "2005", "someone","10");
        Movie movie3 = new Movie("Fast and furious 6","2013","Paul?","N/A" )  ;

        Movie movie4 = new Movie("Harry Potter 1","2001","J.K.Rowling","8");
        Movie movie5 = new Movie("Harry Potter 2","2003","J.K.Rowling","8");
        Movie movie6 = new Movie("Harry Potter 3","2005","J.K.Rowling","8");

        Movie movie7 = new Movie("Fast and furious 4","2009","someone","8");
        Movie movie8 = new Movie("Harry Potter 4","2007","J.K.Rowling","8");
        Movie movie9 = new Movie("Fast and furious 3","2007","Paul?","N/A" )  ;

        Movie movie10 = new Movie("Harry Potter 5","2009","J.K.Rowling","8");
        Movie movie11 = new Movie("Harry Potter 6","2011","J.K.Rowling","4");
        Movie movie13 = new Movie("Harry Potter 7","2013","J.K.Rowling","2");

        Movie movie12 = new Movie("The BatMan","2013","someone","7");
        Movie movie14 = new Movie("The Words", "2009", "someone","1");
        Movie movie15 = new Movie("Iron Man 3","2013","Paul?","N/A" )  ;

        addMovieToList(movie);
        addMovieToList(movie1);
        addMovieToList(movie2);
        addMovieToList(movie3);
        addMovieToList(movie4);
        addMovieToList(movie5);
        addMovieToList(movie6);
        addMovieToList(movie7);
        addMovieToList(movie8);
        addMovieToList(movie9);
        addMovieToList(movie10);
        addMovieToList(movie11);
        addMovieToList(movie12);
        addMovieToList(movie13);
        addMovieToList(movie14);
        addMovieToList(movie15);
    }

    private void LoadUsers()
    {
        User u1 = new User("admin","admin");
        User u2 = new User("username","password");
        User u3 = new User("Jone Doe","password","jd@hotmail.com","02982748274");
    }

    public void ShowScreen()
    {
       System.out.println("Welcome to Bangalore Public Library");
    }

    public void ShowMenu()  //before login
    {
        if(userLoggedIn)
        {
            ShowMainMenu();
        }
        else
        {
            System.out.println("1.Login");
            System.out.println("2.View book list");
            System.out.println("3.View movie list");
            ExecuteUserCommand(GetUserSelection());
        }
    }

    public void ShowMainMenu() //after login
    {
        System.out.println("1.Log out");
        System.out.println("2.View book list");
        System.out.println("3.View movie list");
        System.out.println("4.Reserve a book");
        System.out.println("5.View my profile");
        System.out.println("6.Quit");
        System.out.println();
        System.out.println("Please use number 1 - 6 to make selections:");
        ProcessUserActions();
    }

    private void ProcessUserActions()
    {
        String choice = GetUserSelection();

        ExecuteUserCommand(choice);

        if (!choice.equals("6"))
        {
            ShowMainMenu();
        }

    }

    private  void ExecuteUserCommand(String userChoice)
    {
        int choice = 0;
        try
        {
            choice = Integer.parseInt(userChoice);
        }
        catch(Exception e)
        {
            System.out.println("Invalid option. Please enter number 0-6");
            ShowMenu();
        }

            switch (choice)
            {
                case 1:

                    if(userLoggedIn)
                    {
                        System.out.println("Log out function is not implemented....yet.");
                    }
                    else
                    {
                        Login();
                    }
                    break;
                case 2:
                    ShowBookList();
                    break;
                case 3:
                    ShowMovieList();
                    break;
                case 4:
                    ReserveBook();
                    break;
                case 5:
                    ShowUserProfile();
                    break;
                case 6:
                    System.out.println("Thank you for using Bangalore Public library application.");
                    System.out.println("Bye.");
                    break;
                default:
                    System.out.println("Invalid option. Please enter a valid number.");
                    System.out.println();
                    break;
            }

    }

    public void Login()//(String userName, String password)
    {
        String userName;
        String password;

        System.out.println("Please input username: ");
        userName = GetUserSelection();

        System.out.println("Please enter password: ");
        password = GetUserSelection();

        System.out.println("logging in..");

        currentUser = User.login(userName,password);

        if(currentUser == null)
        {
            System.out.println("**************************************");
            System.out.println("Please talk to a Librarian. Thank you.");
            System.out.println("**************************************");

            ShowMenu();
        }
        else
        {
            System.out.println("Logged in as: " + currentUser.getUserName()
                                +". Email: " + currentUser.getEmail() +
                                ". Contact number:" + currentUser.getPhoneNumber());

            userLoggedIn = true;

            ShowMainMenu();
        }

    }

    public void ShowBookList()
    {
        System.out.println("----------------------------------");

        for(LibraryBook book:bookList)
        {
            System.out.println(bookList.indexOf(book) + ". " + book.GetBook().GetBookName());
        }

        System.out.println("----------------------------------");
    }

    public  void ShowMovieList()
    {
        System.out.println("----------------------------------");
        Movie header = movieList.get(0);
        System.out.println(header.getName() + "    " + header.getYear()+"    "
                            +header.getAuthor()+ "    "+header.getRating());
        for(int i=1;i<movieList.size();i++)
        {
            Movie temp = movieList.get(i);
            System.out.println
                    (i + ". "+temp.getName()+"    "+temp.getYear() +"    "
                            +temp.getAuthor()+"    "+temp.getRating());
        }
        System.out.println("----------------------------------");
    }

    public ArrayList<LibraryBook> GetLibraryBookList()
    {
        return bookList;
    }

    public void ReserveBook()
    {
        ShowBookList();
        System.out.println("Please enter the index of the book that you want to reserve:");
        int bookIndex = Integer.parseInt(GetUserSelection());
        if(bookIndex >= bookList.size())
        {
            System.out.println("Invalid book index. Please enter a valid number.");
            System.out.println();
        }

        LibraryBook libraryBook = bookList.get(bookIndex);
        if(libraryBook.BookIsBorrowed()==false && libraryBook.BookIsReserved()==false)
        {
            libraryBook.SetBookBorrowed(true);
            libraryBook.SetBookReserved(true);
            System.out.println("Thank You! Enjoy the book.");
        }
        else
        {
            System.out.println("Sorry we don't have that book yet.");
        }
    }

    private void ShowUserProfile()
    {
        if(userLoggedIn)
        {
            System.out.println(currentUser.getUserName()
                    +". Email: " + currentUser.getEmail() +
                    ". Contact number:" + currentUser.getPhoneNumber());

        }
        else
        {
            System.out.println("**************************************");
            System.out.println("Please talk to a Librarian. Thank you.");
            System.out.println("**************************************");
        }
    }

    private String GetUserSelection()
    {
        userInput = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(userInput);
        String userInputValue = null;

        try
        {
            userInputValue = reader.readLine().trim().toLowerCase();
        }
        catch (Exception e)
        {
            System.out.println("Invalid option. Please enter a valid number.");
        }
        return userInputValue;
    }

    public void addBookToList(Book book)
    {
        LibraryBook libraryBook = new LibraryBook(book,false,false,new User("Admin", "1"));
        if(bookList == null)
        {
            bookList = new ArrayList<LibraryBook>();
        }
        bookList.add(libraryBook)  ;
    }

    public void addMovieToList(Movie movie)
    {
        if(movieList == null)
        {
            movieList = new ArrayList<Movie>();
        }

        movieList.add(movie);
    }

}
